#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>

int main(int argc, char **argv)
{
    int fd;
    unsigned char key_vals;
    int ret = 0;

    struct pollfd fds[1]; //可以同时查询多个文件是否触发

    fd = open("/dev/key0", O_RDWR);
    if(fd < 0)
    {
        printf("can't open!\n");
        return 0;
    }

    fds[0].fd = fd; //查询fd文件
    fds[0].events = POLLIN; // POLLIN 有数据等待读取
    while(1)
    {
        // 需要查询的文件数组， 查询的文件数， 超时时间ms
        ret = poll(fds, 1, 3000);
        if(ret)
        {
            read(fd, &key_vals, 1);
            printf("key = %x\n",key_vals);
        }
        else
        {
            printf("timeout\n");
        }
    }

    return 0;
}
